package hu.myai.gui.widgets.model;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;

public class LabelRenderer implements PaintListener {
    private double x;
    private double y;

    private ILabelAdapter adapter;

    private int alignment = ALIGN_LEFT;
    private int relativeTo = TOP_LEFT;

    private Color backgroundColor;
    private Color foregroundColor;
    private Color borderColor;
    private boolean paintBackground = true;
    private boolean paintBorder = false;

    @Override
    public void paintControl(PaintEvent e) {
        if (adapter != null) {
            int w = e.width;
            int h = e.height;

            int px = (int) (x * w);
            int py = (int) (y * h);
            String[] data = adapter.getText().split("\n");
            for (String s : data) {
                Point size = e.gc.textExtent(s);
                int px1 = px;
                int py1 = py;

                if (relativeTo == TOP_LEFT) {
                    if (alignment == ALIGN_RIGHT) {
                        px1 = px - size.x;
                    } else if (alignment == ALIGN_CENTER) {
                        px1 = px - size.x / 2;
                    }
                } else if (relativeTo == TOP_RIGHT) {
                    if (alignment == ALIGN_LEFT) {
                        px1 = px - size.x;
                    } else if (alignment == ALIGN_CENTER) {
                        px1 = px - size.x / 2;
                    }
                    px1 = w - 3 - px1 - size.x;
                } else if (relativeTo == BOTTOM_LEFT) {
                    if (alignment == ALIGN_RIGHT) {
                        px1 = px - size.x;
                    } else if (alignment == ALIGN_CENTER) {
                        px1 = px - size.x / 2;
                    }
                    py1 = h - 3 - py1 - size.y;
                } else if (relativeTo == BOTTOM_RIGHT) {
                    if (alignment == ALIGN_LEFT) {
                        px1 = px - size.x;
                    } else if (alignment == ALIGN_CENTER) {
                        px1 = px - size.x / 2;
                    }
                    px1 = w - 3 - px1 - size.x;
                    py1 = h - 3 - py1 - size.y;
                }

                if (paintBackground) {
                    if (backgroundColor != null) {
                        e.gc.setBackground(backgroundColor);
                    }
                    e.gc.fillRectangle(px1, py1, size.x + 2, size.y + 2);
                }

                if (foregroundColor != null) {
                    e.gc.setForeground(foregroundColor);
                }
                e.gc.drawText(s, px1 + 2, py1 + 1);

                if (paintBorder) {
                    if (borderColor != null) {
                        e.gc.setForeground(borderColor);
                    }
                    e.gc.drawRectangle(px1, py1, size.x + 2, size.y + 2);
                }
                py += size.y;
            }
        }
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public ILabelAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(ILabelAdapter adapter) {
        this.adapter = adapter;
    }

    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public Color getForegroundColor() {
        return foregroundColor;
    }

    public void setForegroundColor(Color foregroundColor) {
        this.foregroundColor = foregroundColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public boolean isPaintBackground() {
        return paintBackground;
    }

    public void setPaintBackground(boolean paintBackground) {
        this.paintBackground = paintBackground;
    }

    public boolean isPaintBorder() {
        return paintBorder;
    }

    public void setPaintBorder(boolean paintBorder) {
        this.paintBorder = paintBorder;
    }

    public int getAlignment() {
        return alignment;
    }

    public void setAlignment(int alignment) {
        this.alignment = alignment;
    }

    public int getRelativeTo() {
        return relativeTo;
    }

    public void setRelativeTo(int relativeTo) {
        this.relativeTo = relativeTo;
    }

    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_CENTER = 1;
    public static final int ALIGN_RIGHT = 2;
    public static final int TOP_LEFT = 0;
    public static final int TOP_RIGHT = 1;
    public static final int BOTTOM_LEFT = 2;
    public static final int BOTTOM_RIGHT = 3;
}
